//
//  HLTorderinfoTableViewController.m
//  huiliantong
//
//  Created by hlt on 15/10/14.
//  Copyright © 2015年 hlt. All rights reserved.
//

#import "HLTorderinfoTableViewController.h"

@interface HLTorderinfoTableViewController ()<MONActivityIndicatorViewDelegate>


@property (weak, nonatomic) IBOutlet UIButton *but1;

@property (weak, nonatomic) IBOutlet UIButton *but2;







@property NSString *biaozhi;



@end

@implementation HLTorderinfoTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.jine.text=self.orderinfo[@"money_paid"];
//    self.shopname.text=self.orderinfo[@"shop_name"];
    self.dingdanhao.text=self.orderinfo[@"order_sn"];
    self.date.text=self.orderinfo[@"add_time"];
    self.zhongjia.text=self.orderinfo[@"order_amount"];
    self.zhekou.text=self.orderinfo[@"discount"];
    self.shijijine.text=self.orderinfo[@"money_paid"];
    
    
    NSString *state;
    if ([self.orderinfo[@"shop_order_statue"] isEqual:@"1"]) {
        state=@"可退款";
        [self.but1 setTitle:@"申请退款" forState:UIControlStateNormal];
        
    }
    else if([self.orderinfo[@"shop_order_statue"] isEqual:@"2"])
    {
        state=@"退款中";
      
         [self.but1 setTitle:@"取消退款" forState:UIControlStateNormal];
        
    }else if([self.orderinfo[@"shop_order_statue"] isEqual:@"3"])
    {
        state=@"退款成功";
        //隐藏确认支付按钮
        
    }else if([self.orderinfo[@"shop_order_statue"] isEqual:@"4"])
    {
        state=@"交易完成";
        //隐藏确认支付按钮
       
        
        
        
        
    }else if([self.orderinfo[@"shop_order_statue"] isEqual:@"5"])
    {
        state=@"拒绝退款";
        //隐藏确认支付按钮
        
    }
    self.state.text=state;
    
    _biaozhi=self.orderinfo[@"shop_order_statue"];
    
    
   
    
    
    
}







- (IBAction)but1:(id)sender {
    
    
    
    if ([_biaozhi isEqual:@"2"]) {
        //执行取消退款操作
        Account *acc=[[AccountTool sharedAccountTool] currentAccount];
        
        //取消退款(order_sn=订单号 username=18285117540)
        NSString *url=[quxiaotuikuan stringByAppendingFormat:@"&username=%@&order_sn=%@",acc.custPhone,self.dingdanhao.text];
        [HLTNetWorkingtool getary:url success:^(NSArray *ary) {
            
            if ([ary[0][@"status"] isEqual:@"0"]) {
                
                [self.but1 setTitle:@"申请退款" forState:UIControlStateNormal];
                UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"取消退款成功" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:@"好", nil];
                [alert show];
                self.state.text=@"可退款";
                _biaozhi=@"1";
                
                NSArray *ary= self.parent.parent.viewControllers;
                for (HLTorderTableViewController *order in ary) {
                    
                    [order reloadtable];
                }
                
                
            }
            else{
                
                    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"取消退款失败" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:@"好", nil];
                    [alert show];
                
            }
            
        } faliure:^(NSString *error) {
            
            
        }];
    }
    else
    {
    Account *acc=[[AccountTool sharedAccountTool] currentAccount];
    
    //申请退款(order_sn=订单号 username=18285117540)
    NSString *url=[shenqingtuikuan stringByAppendingFormat:@"&username=%@&order_sn=%@",acc.custPhone,self.dingdanhao.text];
    [HLTNetWorkingtool getary:url success:^(NSArray *ary) {
        
         NSLog(@"%@",ary);
        
        if ([ary[0][@"status"] isEqual:@"0"]) {
            
            _biaozhi=@"2";
            self.state.text=@"退款中";
            [self.but1 setTitle:@"取消退款" forState:UIControlStateNormal];
            UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"申请退款成功" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:@"好", nil];
            [alert show];
            
            NSArray *ary= self.parent.parent.viewControllers;
            for (HLTorderTableViewController *order in ary) {
                
                [order reloadtable];
            }
            
            
            
            
            
        }
        else{
            
            UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"申请退款失败" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:@"好", nil];
            [alert show];
            
        }
        
        
        
    } faliure:^(NSString *error) {
        
        
    }];
    
    
    
    }
    
}




- (IBAction)but2:(id)sender {
    
    Account *acc=[[AccountTool sharedAccountTool] currentAccount];
    
//    支付码确认接口 (user_name=18285117540，amount=金额，serial_number=流水号，payToUser=支付给的商家手机号，shop_id=商铺id，paycode_number=支付码)
    NSString *url=[paykeyqueren stringByAppendingFormat:@"&user_name=%@&amount=%@&serial_number=%@&payToUser=%@&shop_id=%@&paycode_number=%@",acc.custPhone,self.orderinfo[@"order_amount"],self.orderinfo[@"serial_number"],self.orderinfo[@"to_number"],self.orderinfo[@"shop_id"],self.orderinfo[@"paycode_number"]];
    
    
    
    
    UIView *view1=[[UIView alloc] initWithFrame:self.view.bounds];
    view1.backgroundColor=[UIColor grayColor];
    view1.alpha=0.4;
    [self.view addSubview:view1];
    MONActivityIndicatorView *indicatorView = [[MONActivityIndicatorView alloc] init];
    indicatorView.delegate = self;
    indicatorView.numberOfCircles = 3;
    indicatorView.radius = 20;
    indicatorView.internalSpacing = 3;
    indicatorView.center = self.view.center;
    [indicatorView startAnimating];
    [self.view addSubview:indicatorView];
    
    
    
    
    [HLTNetWorkingtool getdic:url success:^(NSDictionary *ary) {
        
        [indicatorView stopAnimating];
        [view1 removeFromSuperview];
        
        //将nsnumber 装换成nsstring
        NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
        NSString *str = [numberFormatter stringFromNumber:ary[@"code"]];
        if ([str isEqual:@"2"]) {
            UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"确认支付成功" message:ary[@"msg"] delegate:nil cancelButtonTitle:nil otherButtonTitles:@"好", nil];
            
            [alert show];
            NSArray *ary= self.parent.parent.viewControllers;
            for (HLTorderTableViewController *order in ary) {
                
                [order reloadtable];
            }
            
            [self.navigationController popViewControllerAnimated:YES]; //返回上一页面 
            
        }else
        {
            UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"确认支付失败" message:ary[@"msg"] delegate:nil cancelButtonTitle:nil otherButtonTitles:@"好", nil];
            
            [alert show];
            
        }
        
      
        
        
        
        
        
    } faliure:^(NSString *error) {
        
        [indicatorView stopAnimating];
        [view1 removeFromSuperview];
    }];

    
    
    
    
}





- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    

    
    if (indexPath.section==0) {
        
        return 127;

    }else if (indexPath.section==1)
    {
        
        
        
        if ([self.orderinfo[@"shop_order_statue"] isEqual:@"1"]) {
            
            [self.but1 setTitle:@"申请退款" forState:UIControlStateNormal];
            return 67;
        }
        else if([self.orderinfo[@"shop_order_statue"] isEqual:@"2"])
        {
            [self.but1 setTitle:@"取消退款" forState:UIControlStateNormal];
            return 67;
        }else if([self.orderinfo[@"shop_order_statue"] isEqual:@"3"])
        {
           
            return 0;
            
        }else if([self.orderinfo[@"shop_order_statue"] isEqual:@"4"])
        {
            return 0;
        }
        else
        {
            self.but1.hidden=YES;
            return 67;
           
            
        }
    }else
    {
        if (indexPath.row==0) {
            
             return 64;
        }else
        {
            if ([self.orderinfo[@"shop_order_statue"] isEqual:@"1"]) {
                
                [self.but1 setTitle:@"申请退款" forState:UIControlStateNormal];
                
                return   screen_height-127-67-64-64;
            }
            else if([self.orderinfo[@"shop_order_statue"] isEqual:@"2"])
            {
                [self.but1 setTitle:@"取消退款" forState:UIControlStateNormal];
                return   screen_height-127-67-64-64;
            }else if([self.orderinfo[@"shop_order_statue"] isEqual:@"3"])
            {
                
                return   screen_height-127-64-64;
                
            }else if([self.orderinfo[@"shop_order_statue"] isEqual:@"4"])
            {
                return   screen_height-127-64-64;
            }
            else
            {
                return   screen_height-127-64-64-67;
                
            }
        
        }
        
       

    }
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




#pragma mark - MONActivityIndicatorViewDelegate Methods

- (UIColor *)activityIndicatorView:(MONActivityIndicatorView *)activityIndicatorView
      circleBackgroundColorAtIndex:(NSUInteger)index {
    CGFloat red   = (arc4random() % 256)/255.0;
    CGFloat green = (arc4random() % 256)/255.0;
    CGFloat blue  = (arc4random() % 256)/255.0;
    CGFloat alpha = 1.0f;
    return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}


@end
